// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

// Compressed stream implementations

#pragma once

#include <memory>
#include <string>

#include <nebula/io/concurrency.h>
#include <nebula/io/interfaces.h>
#include <turbo/base/macros.h>
#include <nebula/compression/compression.h>

namespace nebula {

    class MemoryPool;

}  // namespace nebula

namespace nebula::io {

    class TURBO_EXPORT CompressedOutputStream : public OutputStream {
    public:
        ~CompressedOutputStream() override;

        /// \brief create a compressed output stream wrapping the given output stream.
        ///
        /// The codec must be capable of streaming compression. Some codecs,
        /// like Snappy, are not able to do so.
        static turbo::Result<std::shared_ptr<CompressedOutputStream>> create(
                Codec *codec, const std::shared_ptr<OutputStream> &raw,
                MemoryPool *pool = default_memory_pool());

        // OutputStream interface

        /// \brief close the compressed output stream.  This implicitly closes the
        /// underlying raw output stream.
        turbo::Status close() override;

        turbo::Status abort() override;

        bool closed() const override;

        turbo::Result<int64_t> tell() const override;

        turbo::Status write(const void *data, int64_t nbytes) override;
        /// \cond FALSE
        using Writable::write;

        /// \endcond
        turbo::Status flush() override;

        /// \brief Return the underlying raw output stream.
        std::shared_ptr<OutputStream> raw() const;

    private:
        TURBO_DISALLOW_COPY_AND_ASSIGN(CompressedOutputStream);

        CompressedOutputStream() = default;

        class TURBO_NO_EXPORT Impl;

        std::unique_ptr<Impl> impl_;
    };

    class TURBO_EXPORT CompressedInputStream
            : public internal::InputStreamConcurrencyWrapper<CompressedInputStream> {
    public:
        ~CompressedInputStream() override;

        /// \brief create a compressed input stream wrapping the given input stream.
        ///
        /// The codec must be capable of streaming decompression. Some codecs,
        /// like Snappy, are not able to do so.
        static turbo::Result<std::shared_ptr<CompressedInputStream>> create(
                Codec *codec, const std::shared_ptr<InputStream> &raw,
                MemoryPool *pool = default_memory_pool());

        // InputStream interface

        bool closed() const override;

        turbo::Result<std::shared_ptr<const KeyValueMetadata>> read_metadata() override;

        Future<std::shared_ptr<const KeyValueMetadata>> read_metadata_async(
                const IOContext &io_context) override;

        /// \brief Return the underlying raw input stream.
        std::shared_ptr<InputStream> raw() const;

    private:
        friend InputStreamConcurrencyWrapper<CompressedInputStream>;
        TURBO_DISALLOW_COPY_AND_ASSIGN(CompressedInputStream);

        CompressedInputStream() = default;

        /// \brief close the compressed input stream.  This implicitly closes the
        /// underlying raw input stream.
        turbo::Status DoClose();

        turbo::Status DoAbort() override;

        turbo::Result<int64_t> DoTell() const;

        turbo::Result<int64_t> DoRead(int64_t nbytes, void *out);

        turbo::Result<std::shared_ptr<Buffer>> DoRead(int64_t nbytes);

        class TURBO_NO_EXPORT Impl;

        std::unique_ptr<Impl> impl_;
    };

}  // namespace nebula::io

